package com.wkedu.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkedu.repair.dao.RepairStudentMapper;
import com.wkedu.repair.dao.RepairTeacherMapper;
import com.wkedu.repair.dao.StudentMapper;
import com.wkedu.repair.dao.SupervisorMapper;
import com.wkedu.repair.dto.*;
import com.wkedu.repair.entity.RepairStudent;
import com.wkedu.repair.entity.RepairTeacher;
import com.wkedu.repair.entity.Supervisor;
import com.wkedu.repair.service.SupervisorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/9/5 10:47 上午
 */
@Transactional
@Service
public class SupervisorServiceImpl implements SupervisorService {

    @Autowired
    private SupervisorMapper supervisorMapper;

    @Autowired
    private RepairStudentMapper repairStudentMapper;

    @Autowired
    private RepairTeacherMapper repairTeacherMapper;

    @Override
    public void addSupervisor(SupervisorDto supervisorDto) {

        Supervisor supervisor = new Supervisor();
        supervisor.setSupervisorId(supervisorDto.getSupervisorId());
        supervisor.setName(supervisorDto.getName());
        supervisor.setPassword(supervisorDto.getPassword());
        supervisor.setUsername(supervisorDto.getUsername());
        supervisor.setPhone(supervisorDto.getPhone());

        supervisorMapper.insert(supervisor);

    }

    @Override
    public SupervisorDto getSupervisor(String supervisorId) {

        QueryWrapper<Supervisor> supervisorQueryWrapper = new QueryWrapper<>();
        supervisorQueryWrapper.eq("supervisor_id",supervisorId);

        Supervisor supervisor = supervisorMapper.selectOne(supervisorQueryWrapper);

        if (supervisor != null){
            SupervisorDto supervisorDto = new SupervisorDto(
                    supervisor.getName(), supervisor.getUsername(), supervisor.getPassword()
                    , supervisor.getPhone(), supervisor.getSupervisorId()
            );
            return supervisorDto;
        }else {
            return null;
        }
    }

    @Override
    public SupervisorDto getSupervisorForUsername(String username) {
        QueryWrapper<Supervisor> supervisorQueryWrapper = new QueryWrapper<>();
        supervisorQueryWrapper.eq("username",username);

        Supervisor supervisor = supervisorMapper.selectOne(supervisorQueryWrapper);

        if (supervisor != null){
            SupervisorDto supervisorDto = new SupervisorDto(
                    supervisor.getName(), supervisor.getUsername(), supervisor.getPassword()
                    , supervisor.getPhone(), supervisor.getSupervisorId()
            );
            return supervisorDto;
        }else {
            return null;
        }
    }

    @Override
    public List<RepairStudentDto> listRepairStudentDto() {

        List<RepairStudent> repairStudentList = repairStudentMapper.selectList(null);

        if ( !repairStudentList.isEmpty() ) {
            List<RepairStudentDto> repairStudentDtoList = repairStudentList.stream().map(i -> new RepairStudentDto(
                            i.getRepairInfo(), i.getRepairPath(), i.getRepairType(),
                            i.getStudentId()
                    )
            ).collect(Collectors.toList());

            return repairStudentDtoList;
        } else {


            return null;
        }

    }

    @Override
    public List<RepairTeacherDto> listRepairTeacherDto() {

        List<RepairTeacher> repairTeacherList = repairTeacherMapper.selectList(null);

        if ( !repairTeacherList.isEmpty() ) {
            List<RepairTeacherDto> repairTeacherDtoList = repairTeacherList.stream().map(i -> new RepairTeacherDto(
                    i.getRepairInfo(), i.getRepairPath(), i.getRepairType(),
                    i.getTeacherId()
            )).collect(Collectors.toList());

            return repairTeacherDtoList;

        }else {
            return null;
        }


    }

}
